﻿using MediatR;
using W.Warehouse.Domain;
using W.Warehouse.Domain.DTO;
using W.Warehouse.Read.Api.Application.Command.WarehouseCommands;
using W.Warehouse.Repository.Impl;
using W.Warehouse.Repository.Interface;

namespace W.Warehouse.Read.Api.Application.CommandHandlder.WarehouseCommandsHandlder
{
    public class WarehouseSelectCommandHandlder : IRequestHandler<WarehouseSelectCommand, ApiResult<WarehousePageDTO>>
    {
        private readonly ILogger<WarehouseSelectCommandHandlder> _logger;
        private readonly IWarehouseInfoRepository _warehouseInfoRepository;
        private readonly IWarehouseTypeRepository _warehouseTypeRepository;
        public WarehouseSelectCommandHandlder(IWarehouseInfoRepository warehouseInfoRepository, ILogger<WarehouseSelectCommandHandlder> logger, IWarehouseTypeRepository warehouseTypeRepository)
        {
            _warehouseInfoRepository = warehouseInfoRepository;
            _logger = logger;
            _warehouseTypeRepository = warehouseTypeRepository;
        }
        public async Task<ApiResult<WarehousePageDTO>> Handle(WarehouseSelectCommand request, CancellationToken cancellationToken)
        {
            ApiResult<WarehousePageDTO> result = new ApiResult<WarehousePageDTO>();
            try
            {
                _logger.LogInformation("这是查询显示分页仓库功能");
                WarehousePageDTO warehouse = new WarehousePageDTO();
                var list = from a in await _warehouseInfoRepository.GetAllAsync()
                           join b in await _warehouseTypeRepository.GetAllAsync() on a.WarehouseTypeId equals b.WarehouseTypeId
                           where a.IsDelete == false
                           select new WarehouseInfos
                           {
                               WarehouseId = a.WarehouseId,
                               WarehouseNo = a.WarehouseNo,
                               WarehouseName = a.WarehouseName,
                               LeaseDate = a.LeaseDate,
                               WarehouseTypeName = b.WarehouseTypeName,
                               DepartmentId = a.DepartmentId,
                               WhetherDisable = a.WhetherDisable,
                               WhetherDisableText = a.WhetherDisable.ToString(),
                               WhetherDefault = a.WhetherDefault,
                               WhetherDefaultText = a.WhetherDefault.ToString(),
                               WarehouseAddress = a.WarehouseAddress,
                               WarehouseArea = a.WarehouseArea,
                               WarehouseContact = a.WarehouseContact,
                               WarehousePhone = a.WarehousePhone,
                               WarehouseTypeId = a.WarehouseTypeId,
                               CreateTime = a.CreateTime,
                           };
                if (!string.IsNullOrEmpty(request.warehouseNo))
                {
                    list = list.Where(x => x.WarehouseNo.Contains(request.warehouseNo));
                }
                if (!string.IsNullOrEmpty(request.warehouseName))
                {
                    list = list.Where(x => x.WarehouseName.Contains(request.warehouseName));
                }
                if (request.departmentId > 0)
                {
                    list = list.Where(x => x.DepartmentId == request.departmentId);
                }
                if (request.warehouseTypeId > 0)
                {
                    list = list.Where(x => x.WarehouseTypeId == request.warehouseTypeId);
                }
                warehouse.TotalCount = list.Count();
                warehouse.TotalPage = (int)Math.Ceiling(warehouse.TotalCount * 1.0 / request.pageSize);
                warehouse.List = list.OrderByDescending(x => x.CreateTime).Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize).ToList();
                result.Data = warehouse;
            }
            catch (Exception el)
            {
                result.Code = -1;
                result.Message = el.Message;
                _logger.LogError(el, el.Message);
                throw;
            }
            return result;
        }
    }
}
